package com.yunxi.service.localService.sys.impl;

import com.github.pagehelper.PageInfo;
import com.yunxi.core.base.Constant;
import com.yunxi.core.page.PageHelperPlus;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.core.util.DESUtil;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.dao.sys.UserMapper;
import com.yunxi.model.sys.RoleUser;
import com.yunxi.model.sys.User;
import com.yunxi.service.localService.sys.RoleUserService;
import com.yunxi.service.localService.sys.UserService;
import com.yunxi.shiro.PasswordHelper;
import com.yunxi.sys.pojo.UserVO;
import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuruzi on 2017/5/3.
 */
@Service
public class UserServiceImpl extends ServiceImplPlus<UserMapper, User> implements UserService {
    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();

    @Autowired
    private PasswordHelper passwordHelper;
    @Value("${password.algorithmName}")
    private String algorithmName = "md5";
    @Value("${password.hashIterations}")
    private String hashIterations = "2";

    @Autowired
    RoleUserService roleUserService;

    public UserVO saveCas(UserVO user, LoginInfo sysUser) throws Exception {
        User u = new User();
        if (user.getUserId() == null) {
            //插入用户表
            user.setStatus(1);
            user.setSalt(randomNumberGenerator.nextBytes().toHex());

            String newPassword = DESUtil.encrypt("aiways");
            user.setPassword(newPassword);
            BeanUtils.copyProperties(user,u);
            baseMapper.insert(u);
        }else{
            BeanUtils.copyProperties(user,u);
            baseMapper.updateById(u);
        }
        return user;
    }

    public UserVO insertByPartner(UserVO user) {
        User u = new User();
        if (user.getUserId() == null) {
            //插入用户表
            user.setStatus(1);
            user.setSalt(randomNumberGenerator.nextBytes().toHex());
            String newPassword = new SimpleHash(
                    algorithmName,
                    "aiways",
                    ByteSource.Util.bytes(user.getUserCode() + user.getSalt() + Constant.MD5_KEY),
                    Integer.parseInt(hashIterations)).toHex();
            user.setPassword(newPassword);

            BeanUtils.copyProperties(user,u);
            baseMapper.insert(u);

            RoleUser ru = new RoleUser();
            ru.setRoleId("partnerAdmin");
            ru.setUserId(u.getUserId());
            roleUserService.save(ru);
        }
        return user;
    }

    @Override
    public UserVO getCasId(String id) {
        return baseMapper.getCasId(id);
    }

    @Override
    public UserVO findUserByUserCode(Map map) {
        return baseMapper.getByMapCus(map);
    }

    @Override
    public PageInfo<User> findPageByList(Map<String, Object> map) {
        //设置分页参数
        PageHelperPlus.startPage(map);
        //得到查询list
        List<User> list = baseMapper.findPageByList(map);
        return new PageInfo<User>(list);
    }

    @Override
    public void resetPassword(UserVO user) throws Exception {
        String salt = user.getSalt();
        String passWord = DESUtil.encrypt("123456");
        user.setPassword(passWord);
        User u = new User();
        BeanUtils.copyProperties(user,u);
        baseMapper.updateById(u);
    }

    @Override
    public void updatePassword(UserVO user) throws Exception {
        User u = new User();
        BeanUtils.copyProperties(user,u);
        u.setPassword(DESUtil.encrypt(user.getPassword()));
        //passwordHelper.encryptPassword(u);
        baseMapper.updateById(u);
    }

    @Override
    public UserVO findPdaUserByUserCode(Map map) {
        return baseMapper.findPdaUserByUserCode(map);
    }

    @Override
    public UserVO getByUserCode(String accountNo) {
        Map map = new HashMap();
        map.put("userCode",accountNo);
        return baseMapper.getByUserCode(map);
    }
}
